<template>
    <div class="app-container">

        <!-- 
        <el-row :gutter="10" class="mb8">

            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['business:guest:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['business:guest:remove']">删除</el-button>
            </el-col>

            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['business:guest:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row> -->

        <!-- <el-table v-loading="loading" :data="dormitoryList" @selection-change="handleSelectionChange" border>
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="楼栋" align="center" prop="buildingName" />
            <el-table-column label="房号" align="center" prop="dormitorycode" />
            <el-table-column label="容纳人数" align="center" prop="capacity" />
            <el-table-column label="申请信息" align="center" prop="applicant" />
            <el-table-column label="房客信息" align="center" prop="tenant" />
            <el-table-column label="入住时间" align="center" prop="checkin" width="120px">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.checkin, '{y}-{m}-{d} {h}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="实际入住人" align="center" prop="actualperson" />
            <el-table-column label="联系电话" align="center" prop="actualpersontel" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120px">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-circle-plus-outline" @click="handleAddguest(scope.row)"
                        v-hasPermi="['business:guest:add']" v-if="scope.row.checkin == null">开房</el-button>
                    <el-button size="mini" type="text" icon="el-icon-remove-outline" @click="handleUpdateguest(scope.row)"
                        v-hasPermi="['business:guest:add']" v-if="scope.row.checkin != null">退房</el-button>
                    <el-button size="mini" type="text" @click="handleHis(scope.row)"
                        v-hasPermi="['business:guest:add']">历史</el-button>
                </template>
            </el-table-column>
        </el-table> -->

        <el-row :gutter="10">
            <el-col :span="4" v-for="room in dormitoryList" :key="room.id">
                <el-card style="margin-top: 10px;min-height: 200px;" :body-style="{ padding: '20px', background: room.checkin == null ? '' : '#f0f0f0' }">
                    <div slot="header">
                        <span>{{ room.buildingName }}-{{ room.dormitorycode }}</span>
                        <div style="float: right;">
                            容纳： {{ room.capacity }}
                        </div>
                    </div>
                    <div style="min-height: 100px;">
                        <!-- <p class="label">房间号：<strong>{{ room.dormitorycode }}</strong></p>
                        <p class="label">可容纳人数：<strong>{{ room.capacity }}</strong></p> -->
                        <p class="label" v-if="room.checkin == null" style="text-align: center;"><strong>空房间</strong></p>
                        <p class="label" v-if="room.checkin != null">入住人姓名：<strong>{{ room.actualperson }}</strong></p>
                        <p class="label" v-if="room.checkin != null">联系电话：<strong>{{ room.actualpersontel }}</strong></p>
                        <p class="label" v-if="room.checkin != null">入住时间：<strong>{{ parseTime(room.checkin,
                            '{y}-{m}-{d} {h}') }}</strong></p>

               
                
                    </div>
                    <div class="card-footer">
                            <el-button size="mini" type="text" icon="el-icon-check" @click="handleAddguest(room)"
                            v-if="room.checkin == null">入住</el-button>
                        <el-button size="mini" type="text" icon="el-icon-circle-close" @click="handleUpdateguest(room)"
                            v-if="room.checkin != null">退房</el-button>
                        <el-button size="mini" type="text" @click="handleHistory(room)">历史</el-button>
                        </div>
                </el-card>
            </el-col>
        </el-row>

        <!-- 添加或修改宿舍对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                <el-form-item label="楼栋名称" prop="buildingName">
                    <el-select v-model="form.buildingName" placeholder="请选择楼栋" style="width:200px">
                        <el-option v-for="dict in dict.type.loudong" :key="dict.value" :label="dict.label"
                            :value="dict.value"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="楼层" prop="floor">
                    <el-input-number v-model="form.floor" placeholder="请输入楼层" />
                </el-form-item>
                <el-form-item label="宿舍编号" prop="dormitoryId">
                    <el-input v-model="form.dormitoryId" placeholder="请输入宿舍编号" style="width:200px" />
                </el-form-item>
                <el-form-item label="性别" prop="gender">
                    <el-radio-group v-model="form.gender">
                        <el-radio label="男">男</el-radio>
                        <el-radio label="女">女</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="是否外包宿舍" prop="outsource">
                    <el-radio-group v-model="form.outsource">
                        <el-radio label="否">否</el-radio>
                        <el-radio label="是">是</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="容纳人数" prop="capacity">
                    <el-input-number v-model="form.capacity" placeholder="请输入容纳人数" />
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
        <!-- 添加或修改招待所使用记录对话框 -->
        <el-dialog :title="guesttitle" :visible.sync="guestopen" width="800px" append-to-body>
            <el-form ref="guestform" :model="guestform" :rules="guestrules" label-width="100px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="使用申请" prop="applicant">
                            <el-input v-model="guestform.applicant" placeholder="请输入使用申请" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="房客信息" prop="tenant">
                            <el-input v-model="guestform.tenant" placeholder="请输入房客信息" />
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="12">
                        <el-form-item label="入住时间" prop="checkin">
                            <el-date-picker clearable v-model="guestform.checkin" type="datetime"
                                value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择入住时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="使用天数" prop="expectedusedays">
                            <el-input-number v-model="guestform.expectedusedays" placeholder="请输入预计使用天数" />
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="12">
                        <el-form-item label="退房时间" prop="checkout">
                            <el-date-picker clearable v-model="guestform.checkout" type="datetime"
                                value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择退房时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="引领人" prop="leader">
                            <el-input v-model="guestform.leader" placeholder="请输入引领人" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="钥匙领取人" prop="keyrecipient">
                            <el-input v-model="guestform.keyrecipient" placeholder="请输入钥匙领取人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="联系方式" prop="keyrecipienttel">
                            <el-input v-model="guestform.keyrecipienttel" placeholder="请输入联系方式" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="实际入住人" prop="actualperson">
                            <el-input v-model="guestform.actualperson" placeholder="请输入实际入住人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="联系方式" prop="actualpersontel">
                            <el-input v-model="guestform.actualpersontel" placeholder="请输入实际入住人联系方式" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="guestform.remark" placeholder="请输入备注" />
                        </el-form-item>
                    </el-col>

                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button :loading="buttonLoading" type="primary" @click="submitFormguest">确 定</el-button>
                <el-button @click="cancelguest">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
  
<script>
import { listDormitory, getDormitory, delDormitory, addDormitory, updateDormitory } from "@/api/business/dormitory";
import { listGuest, getGuest, delGuest, addGuest, updateGuest, guestlist } from "@/api/business/guest";
import moment from 'moment';
export default {
    name: "Guest",
    dicts: ['loudong'],
    data() {
        return {
            // 按钮loading
            buttonLoading: false,
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 宿舍表格数据
            dormitoryList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
            },
            // 表单参数
            form: {

            },
            // 表单校验
            rules: {
                id: [
                    { required: true, message: "不能为空", trigger: "blur" }
                ],
                dormitoryId: [
                    { required: true, message: "宿舍编号不能为空", trigger: "blur" }
                ],
                buildingName: [
                    { required: true, message: "楼栋名称不能为空", trigger: "change" }
                ],
                capacity: [
                    { required: true, message: "容纳人数不能为空", trigger: "blur" }
                ],
            },
            // 弹出层标题
            guesttitle: "",
            // 是否显示弹出层
            guestopen: false,
            // 表单参数
            guestform: {
                dormitoryid: null,
                checkin: new Date()
            },
            // 表单校验
            guestrules: {
                checkin: [
                    { required: true, message: "入住时间不能为空", trigger: "blur" }
                ],
            },


        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询招待所列表 */
        getList() {
            this.loading = true;
            guestlist(this.queryParams).then(response => {
                this.dormitoryList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        }, cancelguest() {
            this.guestopen = false;
        },
        // 表单重置
        reset() {
            this.form = {
                id: undefined,
                dormitoryId: undefined,
                buildingNumber: undefined,
                buildingName: undefined,
                capacity: 0,
                currentCapacity: undefined,
                adminId: undefined,
                gender: '男',
                outsource: '否'
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.queryParams = {
                pageNum: 1,
                pageSize: 10,
                dormitoryId: undefined,
                buildingNumber: undefined,
                buildingName: undefined,
            },
                this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.dormitoryindex)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.loading = true;
            this.reset();
            const id = row.dormitoryindex || this.ids
            getDormitory(id).then(response => {
                this.loading = false;
                this.form = response.data;
                this.open = true;
                this.title = "修改宿舍";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    this.buttonLoading = true;
                    if (this.form.id != null) {
                        updateDormitory(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        }).finally(() => {
                            this.buttonLoading = false;
                        });
                    } else {
                        addDormitory(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        }).finally(() => {
                            this.buttonLoading = false;
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除宿舍编号为"' + ids + '"的数据项？').then(() => {
                this.loading = true;
                return delDormitory(ids);
            }).then(() => {
                this.loading = false;
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {
            }).finally(() => {
                this.loading = false;
            });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('business/dormitory/export', {
                ...this.queryParams
            }, `dormitory_${new Date().getTime()}.xlsx`)
        },

        // 重置开发表单
        resetguestform() {
            this.guestform = {
                applicant: null,
                tenant: null,
                //checkin: new Date(),
                expectedusedays: 1,
                // checkout: new Date(),
                leader: null,
                keyrecipient: null,
                keyrecipienttel: null,
                actualperson: null,
                actualpersontel: null,
                remark: null,

            };
            this.resetForm("guestform");
        },
        /** 新增按钮操作 */
        handleAddguest(row) {
            this.resetguestform();
            this.guestopen = true;
            this.guesttitle = "开房";
            this.guestform.dormitoryid = row.dormitoryindex;
            this.guestform.checkin = new Date();
            this.guestform.checkout = null;
            this.resetForm("guestform");

        },
        /** 修改按钮操作 */
        handleUpdateguest(row) {
            this.loading = true;
            const id = row.id || this.ids
            getGuest(id).then(response => {
                this.loading = false;
                this.guestform = response.data;
                this.guestform.checkout = new Date();
                this.guestopen = true;
                this.guesttitle = "退房";
            });
        },
        /** 提交按钮 */
        submitFormguest() {
            this.$refs["guestform"].validate(valid => {
                if (valid) {
                    this.buttonLoading = true;
                    if (this.guestform.id != null) {
                        this.guestform.checkout = moment(this.guestform.checkout).format('YYYY-MM-DD HH:mm:ss');
                        updateGuest(this.guestform).then(response => {
                            this.$modal.msgSuccess("退房成功");
                            this.guestopen = false;
                            this.getList();
                        }).finally(() => {
                            this.buttonLoading = false;
                        });
                    } else {
                        this.guestform.checkin = moment(this.guestform.checkin).format('YYYY-MM-DD HH:mm:ss');
                        //console.log(moment(this.guestform.checkin).format('YYYY-MM-DD HH:mm:ss'));
                        addGuest(this.guestform).then(response => {
                            this.$modal.msgSuccess("开房成功");
                            this.guestopen = false;
                            this.getList();
                        }).finally(() => {
                            this.buttonLoading = false;
                        });
                    }
                }
            });
        },
        /** 历史记录 */
        handleHistory(row) {
            const dormitoryid = row.dormitoryindex;
            //this.$router.push("/business/dormitory/guesthisuse/" + dormitoryid);
            this.$router.push({ path: "/business/dormitory/guesthisuse", query: { dormitoryid: dormitoryid } });
        },

    }
};
</script>
  
<style scoped>

.card-footer {
    /* display: flex;
    justify-content: space-between; */
    text-align: right;
    margin-top: 10px;
}
</style>